/**
 	MessageBox component
 */
package nrb.utils
{
	import constant.*;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import mx.containers.*;
	import mx.controls.*;
	import mx.core.Application;
	import mx.managers.PopUpManager;
	
	/**
	 	Type: Class
	 	@Params:none
	 	@return:none
	 	Desc: Class for Message
	 */
	public class MessageBox {
		
		/**
		 	declare variables
		 */
		private var ap:Application;
		private var cb:ApplicationControlBar;
		private var hbox:HBox;
		private var message_title:Label;
		private var message_content:TextArea;
		private var btn_ok:Button;
		private var btn_cancel:Button;
		private var listener:Function;
		private var params:Object = new Object;
		private var language:String = 'en';
		
		private var label:CLSLabel;
		
		
		/**
		 	Type: Controller
		 	@Params:none
		 	@return:none
		 	Desc: 
		 */
		public function MessageBox(obj:*=null) {
			if(obj){
				if(obj.lang)
					language = obj.lang;
			}
			label = new CLSLabel();
			label.set_language(language);
				
		}
		
		/**
		 	Type: Public Function
		 	@Params:obj[{title:'String', message:'String',parent:DisPlayObject, yesnobutton:Bollean, listener:Function=null}] AS Object
		 	@return:void
		 	Desc: shows the messagein center screen
		 */
		public function show_message(obj:Object): void {
			set_setting();
			ap = new Application();
			message_title.text = obj.title;
			message_content.text = obj.message;
			if(obj.params)
				params = obj.params;
			listener = obj.listener;
			cb.addChild(message_title);
			ap.addChild(cb);
			ap.addChild(message_content);
			set_button(obj.yesnobutton);
			ap.addChild(hbox);
			ap.width = 450;
			ap.height = 200;
			
			PopUpManager.addPopUp(ap, obj.parent, true);
			PopUpManager.centerPopUp(ap);
			
			//focus on btn ok
			btn_ok.setFocus();
		}
		
		/**
		 	Type: Private Function
		 	@Params:none
		 	@return:void
		 	Desc: sets the message box properties
		 */
		private function set_setting(): void {
			listener = new Function();
			ap = new Application();
			cb = new ApplicationControlBar();
			hbox = new HBox();
			message_title = new Label();
			message_content = new TextArea();
			
			message_content.percentWidth = 100;
			message_content.percentHeight = 100;
			message_content.wordWrap = true;
			message_content.editable = false;
			message_content.alpha = 0;
			cb.dock = true;
		}
		
		/**
		 	Type: Private Function
		 	@Params:none
		 	@return:void
		 	Desc: sets the button type (OK | OK-CANCEL)
		 */
		private function set_button(flag:Boolean): void {
			if(flag){
				btn_ok = new Button();
				btn_cancel = new Button();
				btn_ok.label = label.OK;
				btn_cancel.label = label.CANCEL;
				btn_ok.width = 100;
				btn_cancel.width = 100;
				hbox.addChild(btn_ok);
				hbox.addChild(btn_cancel);
				btn_ok.addEventListener(MouseEvent.CLICK, listener_click_button(true));
				btn_cancel.addEventListener(MouseEvent.CLICK, listener_click_button(false));
			}else{
				btn_ok = new Button();
				btn_ok.label = label.OK;
				btn_ok.width = 100;
				hbox.addChild(btn_ok);
				btn_ok.addEventListener(MouseEvent.CLICK, listener_click_button(true));
				btn_ok.addEventListener(KeyboardEvent.KEY_DOWN, listener_click_button_keyboard(true));
			}
		}
		
		/**
		 	Type: Private Function
		 	@Params:flag[TRUE | FALSE] AS Boolean
		 	@return:void
		 	Desc: listener for button click
		 */
		private function listener_click_button(flag:Boolean): Function {
			return function (event:MouseEvent): void {
				PopUpManager.removePopUp(ap);
				if(listener != null)
					listener({flag:flag, params:params});
			}
		}
		
		
		private function listener_click_button_keyboard(flag:Boolean): Function {
			return function (event:KeyboardEvent): void {
				if(event.keyCode == 13){
					PopUpManager.removePopUp(ap);
					if(listener != null)
						listener({flag:flag, params:params});
				}
			}
		}
		
		
		
	}
}
